﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PlanningGIS.MetadataEdit
{
    public class MetadataEditUtility
    {

        /// <summary>
        /// 根据经度选择中央经线
        /// </summary>
        /// <param name="dblX"></param>
        /// <returns></returns>
        public static double GetL0(double dblX)
        {
            dblX = dblX * 3600;
            if (dblX > 437400)//123带
            {
                return 123;
            }
            else if (dblX > 429600 && dblX < 437400)//120带
            {
                return 120;
            }
            else if (dblX > 415800 && dblX < 429600)//117带
            {
                return 117;
            }
            else
            {
                return 0;
            }
        }


        /// <summary>
        /// 从经纬度求出平面坐标(80)
        /// </summary>
        /// <param name="gb">纬度</param>
        /// <param name="gl">经度</param>
        /// <param name="X">X</param>
        /// <param name="Y">Y</param>
        /// <param name="L0">中央经线</param>
        public static void BL2XY80(double gb, double gl, out double X, out double Y, double L0)
        {
            double a, aa, bb, e, e1, c, p, t, N, v, nn, m, y1, x0, bbb, lll, C2, C1, C3;
            double b, l;
            double Part1, Part2, Part3, Part4;

            b = gb;
            l = gl - L0;
            bbb = b * 3.1415926535898 / 180;
            lll = l * 3.1415926535898 / 180;
            a = 6378140;
            e = 6.69438499958795E-03;
            e1 = 6.73950181947293E-03;
            c = 6399596.65198801;
            C1 = 32009.8575;
            C2 = 133.9602;
            C3 = 0.6976;
            p = 206264.8062471;
            t = Math.Tan(bbb);
            N = Math.Sqrt(e1 * Math.Pow(Math.Cos(bbb), 2));
            v = Math.Sqrt(1 + Math.Pow(N, 2));
            nn = c / v;
            m = l * Math.Cos(bbb) * 3.1415926535898 / 180.0;
            x0 = 111133.0047 * b - (32009.8575 * Math.Sin(bbb) + 133.9602 * Math.Pow((Math.Sin(bbb)), 3.0) + 0.6976 * Math.Pow((Math.Sin(bbb)), 5.0) + 0.0039 * Math.Pow((Math.Sin(bbb)), 7.0)) * Math.Cos(bbb);
            double g;
            g = 24.0 * (5.0 - Math.Pow(t, 2.0) + 9 * Math.Pow(N, 2.0) + 4 * Math.Pow(N, 4.0));

            Part1 = 1.0 / 24 * (5 - Math.Pow(t, 2.0) + 9 * Math.Pow(N, 2.0) + 4 * Math.Pow(N, 4.0));
            Part2 = 1.0 / 720.0 * (61.0 - 58.0 * Math.Pow(t, 2.0) + Math.Pow(t, 4.0));
            Part4 = (1.0 / 2.0 + (Part1 + Part2 * Math.Pow(m, 2.0)) * Math.Pow(m, 2.0));
            X = x0 + nn * t * (Part4 * Math.Pow(m, 2));
            Part3 = (5.0 - 18.0 * Math.Pow(t, 2.0) + Math.Pow(t, 4.0) + 14.0 * Math.Pow(N, 2.0) - 58.0 * Math.Pow(N, 2.0) * Math.Pow(t, 2.0));
            y1 = nn * ((1.0 + (1.0 / 6.0 * (1.0 - Math.Pow(t, 2.0) + Math.Pow(N, 2.0)) + 1.0 / 120.0 * Part3 * Math.Pow(m, 2.0)) * Math.Pow(m, 2.0)) * m);
            Y = 500000.0 + y1;

        }

    }
}
